package org.easybuy.daoimpol;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.easybuy.dao.BaseDao;
import org.easybuy.dao.CommodityCategoryDao;
import org.easybuy.entity.CommodityCategory;

public class CommodityCategoryDaoImpol extends BaseDao implements
		CommodityCategoryDao {
	// 查询所有一级标题
	@Override
	public List<CommodityCategory> getAllFirstcCategories() {
		ResultSet rs = null;
		List<CommodityCategory> list = new ArrayList<CommodityCategory>();
		String sql = "SELECT *FROM `easybuy_product_category` WHERE `type` = 1";
		CommodityCategory commodityCategory = null;
		rs = this.exxcuteQuery(sql);
		try {
			while (rs.next()) {
				commodityCategory = new CommodityCategory();
				commodityCategory.setId(rs.getInt("id"));
				commodityCategory.setName(rs.getString("name").toString());
				commodityCategory.setParentId(rs.getInt("parentId"));
				list.add(commodityCategory);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(connection, pstmt, rs);
		}
		return list;
	}

	// 根据一级标题查询二级标题
	@Override
	public List<CommodityCategory> getAllSecondCategories(int parentId) {
		ResultSet rs = null;
		List<CommodityCategory> list = new ArrayList<CommodityCategory>();
		String sql = "SELECT *FROM `easybuy_product_category` WHERE `type` = 2 AND parentId = ?";
		CommodityCategory commodityCategory = null;
		rs = this.exxcuteQuery(sql, parentId);
		try {
			while (rs.next()) {
				commodityCategory = new CommodityCategory();
				commodityCategory.setId(rs.getInt("id"));
				commodityCategory.setName(rs.getString("name").toString());
				commodityCategory.setParentId(rs.getInt("parentId"));
				list.add(commodityCategory);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(connection, pstmt, rs);
		}
		return list;
	}

	// 根据一级标题查询三级标题
	@Override
	public List<CommodityCategory> getAllthreatCategories(int parentId) {
		ResultSet rs = null;
		List<CommodityCategory> list = new ArrayList<CommodityCategory>();
		String sql = "SELECT *FROM `easybuy_product_category` WHERE `type` = 3 AND parentId = ?";
		CommodityCategory commodityCategory = null;
		rs = this.exxcuteQuery(sql, parentId);
		try {
			while (rs.next()) {
				commodityCategory = new CommodityCategory();
				commodityCategory.setId(rs.getInt("id"));
				commodityCategory.setName(rs.getString("name").toString());
				commodityCategory.setParentId(rs.getInt("parentId"));
				list.add(commodityCategory);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(connection, pstmt, rs);
		}
		return list;
	}

}
